home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Mac-Source 1994 July
/
Mac-Source_July_1994.iso
/
Other Langs
/
Tickle-4.0 (tcl)
/
src
/
mbtoasd.c
< prev
next >
Wrap
Text File
|
1993-11-18
|
11KB
|
457 lines
/*
** This source code was written by Tim Endres
** Email: time@ice.com.
** USMail: 8840 Main Street, Whitmore Lake, MI 48189
**
** Some portions of this application utilize sources
** that are copyrighted by ICE Engineering, Inc., and
** ICE Engineering retains all rights to those sources.
**
** Neither ICE Engineering, Inc., nor Tim Endres,
** warrants this source code for any reason, and neither
** party assumes any responsbility for the use of these
** sources, libraries, or applications. The user of these
** sources and binaries assumes all responsbilities for
** any resulting consequences.
*/
#pragma segment MACBIN
#include <types.h>
#include <toolutils.h>
#include <standardfile.h>
#include <fcntl.h>
#include <stdio.h>
#include "asd.h"
#include "mb.h"
#include "defines.h"
#undef BLK_SIZE
#define BLK_SIZE 1024
#define iobuffer kBuffer
extern char kBuffer[];
/* static char iobuffer[BLK_SIZE]; */
static long dlength = 0;
static long rlength = 0;
static long cdate = 0;
static long mdate = 0;
/*
** WARNING ! WARNING ! WARNING !
** Both of the parameters to this macro
** are used more than once! Also, I did
** NOT parenthsize any parameters.
*/
#define CHAR4_TO_LONG(char4, long) { \
long = 0; \
long += char4 [0]; long <<= 8; \
long += char4 [1]; long <<= 8; \
long += char4 [2]; long <<= 8; \
long += char4 [3]; \
}
macbinary_to_asingle(crnl_flag)
int crnl_flag;
{
SFReply mbreply, asreply;
Point mypoint;
SFTypeList mytypes;
F_BLK_PTR mbptr;
char *ptr;
int i;
int blocks;
int residue;
int bytes;
int offset;
FILE *mbfile, *asfile;
extern int errno;
mypoint.h = mypoint.v = 75;
MyGetFile(mypoint, "\pMacBinary File:", NULL, -1, mytypes, NULL, &mbreply);
if (!mbreply.good)
return CANCEL;
MyPutFile(mypoint, "\pApple Single File:", "file.as", NULL, &asreply);
if (!asreply.good)
return CANCEL;
p2cstr(asreply.fName);
p2cstr(mbreply.fName);
SetVol(NULL, mbreply.vRefNum);
mbfile = fopen(mbreply.fName, "r");
if (mbfile == NULL) {
Feedback("Error #%d opening macbinary file '%s'.", errno, mbreply.fName);
return FAILURE;
}
SetVol(NULL, asreply.vRefNum);
asfile = fopen(asreply.fName, "w");
if (asfile == NULL) {
Feedback("Error #%d opening apple single file '%s'.", errno, asreply.fName);
fclose(mbfile);
return FAILURE;
}
mbptr = (F_BLK_PTR) &mb_header[0];
bytes = fread(mbptr, 1, MAC_BINARY_HDR_SIZE, mbfile);
if (bytes != MAC_BINARY_HDR_SIZE) {
Feedback("Error #%d getting Macbinary header. Too few bytes.", errno);
fclose(mbfile);
fclose(asfile);
return FAILURE;
}
UBegYield();
RotateCursor(32);
DoYield();
/* mbptr->flags */
CHAR4_TO_LONG(mbptr->dflen, dlength);
CHAR4_TO_LONG(mbptr->rflen, rlength);
CHAR4_TO_LONG(mbptr->cdate, cdate);
CHAR4_TO_LONG(mbptr->mdate, mdate);
Feedback("MacBinary file '%*.*s'",
mbptr->nlen, mbptr->nlen, mbptr->name);
Feedback("File Type '%4.4s'", mbptr->type);
Feedback("File Creator '%4.4s'", mbptr->creator);
Feedback("Data Fork %d bytes.", dlength);
Feedback("Rsrc Fork %d bytes.", rlength);
cdate = MAC_TO_UNIX_TIME(cdate);
Feedback("Creation Date [%d] %s", cdate, ctime(&cdate));
mdate = MAC_TO_UNIX_TIME(mdate);
Feedback("Modification Date [%d] %s", mdate, ctime(&mdate));
RotateCursor(32);
DoYield();
memcpy(&asd_finder, mbptr->type, sizeof(asd_fndr_fork));
asd_file_head.asd_head.magic = MAGIC_APPLE_SINGLE;
asd_file_head.asd_head.version = VERSION_APPLE;
sprintf(asd_file_head.asd_head.filesys, "%-16.16s", "Macintosh");
asd_file_head.asd_head.entries = 3;
i = 0;
asd_file_head.asd_entries[i].id = ASDID_FNDR_FORK;
asd_file_head.asd_entries[i].offset = sizeof(asd_file_hdr);
asd_file_head.asd_entries[i].length = sizeof(asd_fndr_fork);
i++;
asd_file_head.asd_entries[i].id = ASDID_RSRC_FORK;
asd_file_head.asd_entries[i].offset =
asd_file_head.asd_entries[i-1].offset +
asd_file_head.asd_entries[i-1].length;
asd_file_head.asd_entries[i].length = rlength;
i++;
asd_file_head.asd_entries[i].id = ASDID_DATA_FORK;
asd_file_head.asd_entries[i].offset =
asd_file_head.asd_entries[i-1].offset +
asd_file_head.asd_entries[i-1].length;
asd_file_head.asd_entries[i].length = dlength;
RotateCursor(32);
DoYield();
fwrite(&asd_file_head, 1, sizeof(asd_file_hdr), asfile);
fwrite(&asd_finder, 1, sizeof(asd_fndr_fork), asfile);
offset = MAC_BINARY_HDR_SIZE + (((dlength + BLK_SIZE - 1) >> 7) << 7);
Feedback("MBRsrc Offset = %ld Data Length %ld", offset, dlength);
fseek(mbfile, offset, SEEK_SET);
if (rlength > 0) {
blocks = rlength / BLK_SIZE;
residue = rlength % BLK_SIZE;
for ( ; blocks; blocks-- ) {
RotateCursor(32);
DoYield();
if (pause_op)
while (pause_op)
pausing();
bytes = fread(iobuffer, 1, BLK_SIZE, mbfile);
fwrite(iobuffer, 1, BLK_SIZE, asfile);
}
if (residue) {
RotateCursor(32);
DoYield();
if (pause_op)
while (pause_op)
pausing();
bytes = fread(iobuffer, 1, residue, mbfile);
fwrite(iobuffer, 1, residue, asfile);
}
}
fseek(mbfile, MAC_BINARY_HDR_SIZE, SEEK_SET);
if (dlength > 0) {
blocks = dlength / BLK_SIZE;
residue = dlength % BLK_SIZE;
for ( ; blocks; blocks-- ) {
RotateCursor(32);
DoYield();
if (pause_op)
while (pause_op)
pausing();
bytes = fread(iobuffer, 1, BLK_SIZE, mbfile);
if (crnl_flag)
for (i=bytes, ptr=iobuffer; i; i--, ptr++)
if (*ptr == 0x0D) *ptr = 0x0A;
fwrite(iobuffer, 1, BLK_SIZE, asfile);
}
if (residue) {
RotateCursor(32);
DoYield();
if (pause_op)
while (pause_op)
pausing();
bytes = fread(iobuffer, 1, residue, mbfile);
if (crnl_flag)
for (i=bytes, ptr=iobuffer; i; i--, ptr++)
if (*ptr == 0x0D) *ptr = 0x0A;
fwrite(iobuffer, 1, residue, asfile);
}
}
WatchCursorOn();
fclose(mbfile);
fclose(asfile);
UInitCursor();
UEndYield();
return SUCCESS;
}
macbinary_to_adouble(crnl_flag)
int crnl_flag;
{
SFReply mbreply, rreply, dreply;
Point mypoint;
SFTypeList mytypes;
F_BLK_PTR mbptr;
char *ptr;
char orig[64];
int i;
int blocks;
int residue;
int bytes;
int offset;
FILE *mbfile, *rfile, *dfile;
extern int errno;
mypoint.h = mypoint.v = 75;
MyGetFile(mypoint, "\pMacBinary File:", NULL, -1, mytypes, NULL, &mbreply);
if (!mbreply.good)
return CANCEL;
p2cstr(mbreply.fName);
SetVol(NULL, mbreply.vRefNum);
mbfile = fopen(mbreply.fName, "r");
if (mbfile == NULL) {
Feedback("Error #%d opening macbinary file '%s'.", errno, mbreply.fName);
return FAILURE;
}
mbptr = (F_BLK_PTR) &mb_header[0];
bytes = fread(mbptr, 1, MAC_BINARY_HDR_SIZE, mbfile);
if (bytes != MAC_BINARY_HDR_SIZE) {
Feedback("Error #%d getting Macbinary header. (%d != %d)",
errno, bytes, MAC_BINARY_HDR_SIZE);
fclose(mbfile);
return FAILURE;
}
Feedback("MacBinary Name: '%.*s'", mbptr->nlen, mbptr->name);
sprintf(orig, "%.*s", ( (mbptr->nlen > 29) ? 29 : mbptr->nlen ), mbptr->name);
c2pstr(orig);
MyPutFile(mypoint, "\pApple Double Data File:", orig, NULL, &dreply);
if (! dreply.good)
return CANCEL;
sprintf(orig, "%%%.*s",
( (dreply.fName[0] > 26) ? 26 : dreply.fName[0] ), &dreply.fName[1]);
c2pstr(orig);
MyPutFile(mypoint, "\pApple Double Rsrc File:", orig, NULL, &rreply);
if (!rreply.good)
return CANCEL;
p2cstr(rreply.fName);
p2cstr(dreply.fName);
SetVol(NULL, rreply.vRefNum);
rfile = fopen(rreply.fName, "w");
if (rfile == NULL) {
Feedback("Error #%d opening apple double rsrc file '%s'.", errno, rreply.fName);
fclose(mbfile);
return FAILURE;
}
SetVol(NULL, dreply.vRefNum);
dfile = fopen(dreply.fName, "w");
if (dfile == NULL) {
Feedback("Error #%d opening apple double data file '%s'.", errno, dreply.fName);
fclose(mbfile);
fclose(rfile);
return FAILURE;
}
UBegYield();
RotateCursor(32);
DoYield();
/* mbptr->flags */
CHAR4_TO_LONG(mbptr->dflen, dlength);
CHAR4_TO_LONG(mbptr->rflen, rlength);
CHAR4_TO_LONG(mbptr->cdate, cdate);
CHAR4_TO_LONG(mbptr->mdate, mdate);
Feedback("MacBinary file '%*.*s'",
mbptr->nlen, mbptr->nlen, mbptr->name);
Feedback("File Type '%4.4s'", mbptr->type);
Feedback("File Creator '%4.4s'", mbptr->creator);
Feedback("Data Fork %d bytes.", dlength);
Feedback("Rsrc Fork %d bytes.", rlength);
cdate = MAC_TO_UNIX_TIME(cdate);
Feedback("Creation Date [%d] %s", cdate, ctime(&cdate));
mdate = MAC_TO_UNIX_TIME(mdate);
Feedback("Modification Date [%d] %s", mdate, ctime(&mdate));
RotateCursor(32);
DoYield();
memcpy(&asd_finder, mbptr->type, sizeof(asd_fndr_fork));
asd_file_head.asd_head.magic = MAGIC_APPLE_DOUBLE;
asd_file_head.asd_head.version = VERSION_APPLE;
sprintf(asd_file_head.asd_head.filesys, "%-16.16s", "Macintosh");
i = 0;
asd_file_head.asd_entries[i].id = ASDID_FNDR_FORK;
asd_file_head.asd_entries[i].offset = sizeof(asd_file_hdr);
asd_file_head.asd_entries[i].length = sizeof(asd_fndr_fork);
i++;
asd_file_head.asd_entries[i].id = ASDID_RSRC_FORK;
asd_file_head.asd_entries[i].offset =
asd_file_head.asd_entries[i-1].offset +
asd_file_head.asd_entries[i-1].length;
asd_file_head.asd_entries[i].length = rlength;
i++;
#ifdef NEVER_DEFINED
asd_file_head.asd_entries[i].id = ASDID_DATA_FORK;
asd_file_head.asd_entries[i].offset =
asd_file_head.asd_entries[i-1].offset +
asd_file_head.asd_entries[i-1].length;
asd_file_head.asd_entries[i].length = dlength;
i++;
#endif
RotateCursor(32);
DoYield();
asd_file_head.asd_head.entries = i;
fwrite(&asd_file_head, 1, sizeof(asd_file_hdr), rfile);
fwrite(&asd_finder, 1, sizeof(asd_fndr_fork), rfile);
offset = MAC_BINARY_HDR_SIZE + (((dlength + BLK_SIZE - 1) >> 7) << 7);
Feedback("MB-RSRC Offset = %ld Length %ld", offset, rlength);
fseek(mbfile, offset, SEEK_SET);
if (rlength > 0) {
blocks = rlength / BLK_SIZE;
residue = rlength % BLK_SIZE;
for ( ; blocks; blocks-- ) {
RotateCursor(32);
DoYield();
if (pause_op)
while (pause_op)
pausing();
bytes = fread(iobuffer, 1, BLK_SIZE, mbfile);
fwrite(iobuffer, 1, BLK_SIZE, rfile);
}
if (residue) {
RotateCursor(32);
DoYield();
if (pause_op)
while (pause_op)
pausing();
bytes = fread(iobuffer, 1, residue, mbfile);
fwrite(iobuffer, 1, residue, rfile);
}
}
Feedback("MB-DATA Offset = %ld Length %ld", MAC_BINARY_HDR_SIZE, dlength);
fseek(mbfile, MAC_BINARY_HDR_SIZE, SEEK_SET);
if (dlength > 0) {
blocks = dlength / BLK_SIZE;
residue = dlength % BLK_SIZE;
for ( ; blocks; blocks-- ) {
RotateCursor(32);
DoYield();
if (pause_op)
while (pause_op)
pausing();
bytes = fread(iobuffer, 1, BLK_SIZE, mbfile);
if (crnl_flag)
for (i=bytes, ptr=iobuffer; i; i--, ptr++)
if (*ptr == 0x0D) *ptr = 0x0A;
fwrite(iobuffer, 1, BLK_SIZE, dfile);
}
if (residue) {
RotateCursor(32);
DoYield();
if (pause_op)
while (pause_op)
pausing();
bytes = fread(iobuffer, 1, residue, mbfile);
if (crnl_flag)
for (i=bytes, ptr=iobuffer; i; i--, ptr++)
if (*ptr == 0x0D) *ptr = 0x0A;
fwrite(iobuffer, 1, residue, dfile);
}
}
WatchCursorOn();
fclose(mbfile);
fclose(rfile);
fclose(dfile);
UInitCursor();
UEndYield();
return SUCCESS;
}